package com.hyx.bookManager.service.impl;

import com.hyx.bookManager.domain.common.CommonResult;
import com.hyx.bookManager.domain.dto.CurrentBookCount;
import com.hyx.bookManager.domain.dto.TotalPage;
import com.hyx.bookManager.domain.po.Book;
import com.hyx.bookManager.mapper.BookMapper;
import com.hyx.bookManager.mapper.TagMapper;
import com.hyx.bookManager.service.BookService;
import com.hyx.bookManager.utils.FileUtil;
import com.hyx.bookManager.utils.Snowflake;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BookServiceImpl
 * @Description TODO
 * @Author 黄乙轩
 * @Date 2021/6/14 22:51
 * @Version 1.0
 **/

@Service
public class BookServiceImpl implements BookService {

    private final static String IMG_PATH = "F:\\项目\\bookManagerImg\\";

    @Autowired
    BookMapper bookMapper;

    @Autowired
    Snowflake snowflake;

    @Autowired
    FileUtil fileUtil;

    @Autowired
    TagMapper tagMapper;

    @Override
    public CommonResult insertBook(Long tagId, MultipartFile[] file, String name, String desc, Integer num) {
        Long id = snowflake.nextId();
        LocalDateTime createTime = LocalDateTime.now();
        LocalDateTime updateTime = LocalDateTime.now();
        for (MultipartFile multipartFile : file) {
            System.out.println(multipartFile);
        }
        String path = fileUtil.uploadFile(file[0], IMG_PATH, id);
        boolean ans = bookMapper.insertBook(path, name, desc, num, id, createTime, updateTime) & tagMapper.insertBook2Tag(id, tagId);
        return CommonResult.success(ans);
    }

    @Override
    public CommonResult getAllBook(Integer page, Integer pageSize, Long tagId, Byte bookStatus) {
        List<Book> books = bookMapper.getBook((page - 1) * pageSize, pageSize, tagId, bookStatus);
        return CommonResult.success("查询成功", books);
    }

    @Override
    public CommonResult getBookCount(Integer pageSize, String tagId, String status) {
        TotalPage totalPage = new TotalPage();
        Long num = bookMapper.getBookCount(pageSize, tagId, status);
        totalPage.setNum(num);
        totalPage.setPageNum(num / pageSize);
        if (num % pageSize != 0) {
            totalPage.setPageNum(totalPage.getPageNum() + 1);
        } else {
            totalPage.setPageNum(totalPage.getPageNum());
        }
        return CommonResult.success(totalPage);
    }

    @Override
    public CommonResult getMyBorrowingBook(String id, String page, String pageSize) {
        List<Book> books = bookMapper.getBorrowingByUserId(id, (Long.valueOf(page) - 1) * Long.valueOf(pageSize), Long.valueOf(pageSize));
        return CommonResult.success(books);
    }

    @Override
    public CommonResult getBookTotalPage(Long pageSize, String tagId) {
        Long totalPage ;
        if(tagId==null){
            totalPage = bookMapper.getTotalBook();
            if(totalPage%pageSize==0){
                totalPage/=pageSize;
            }else {
                totalPage=totalPage/pageSize+1;
            }
        }else{
            totalPage = bookMapper.getBookTotalPage(pageSize,Long.valueOf(tagId));
        }
        return CommonResult.success(totalPage);
    }

    @Override
    public CommonResult updateBookState(Integer state, Long bookId) {
        Integer row = bookMapper.updateBookState(state, bookId);
        if (row == 1) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.fail("修改失败");
        }
    }

    @Override
    public CommonResult borrowingBook(LocalDateTime now, LocalDateTime after, Long bookId, String id) {
        Integer row = bookMapper.borrowingBook(now, after, id, bookId);
        if (row != null) {
            return CommonResult.success(id);
        } else {
            return CommonResult.fail();
        }
    }

    @Override
    public CommonResult getBookByTagId(Integer page, Integer pageSize, Long tagId) {
        List<Book> books = bookMapper.getBookByTagId((page - 1) * pageSize, pageSize, tagId);
        return CommonResult.success(books);
    }

    @Override
    public CommonResult getCurrentBookData(LocalDateTime befor, LocalDateTime now) {
        List<Book> books = bookMapper.getCurrentBookData(befor, now);
        List<LocalDate> localDates = new ArrayList<>();
        LocalDate date = LocalDate.of(befor.getYear(), befor.getMonth(), befor.getDayOfMonth() + 1);
        List<Long> counts = new ArrayList<>();
        int num = 0;
        for (int i = 0; i < 7; i++) {
            localDates.add(date);
            System.out.println(date);
            if(num>=books.size()){
                counts.add(0L);
            }
            else if (books.get(num).getCreateTime().toLocalDate().equals(date)) {
                System.out.println(num + "," + books.size());
                counts.add(Long.valueOf(books.get(num).getNum()));
                num++;
            }
            else {
                counts.add(0L);
            }
            date = date.plusDays(1);
        }
        CurrentBookCount userCount = new CurrentBookCount();
        userCount.setNum(counts);
        userCount.setTime(localDates);
        return CommonResult.success(userCount);
    }
}
